package com.leetcodehot.problems;

public class problems112 {


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * 一、看好退出条件
     * 二、超级操作
     * 三、微操作
     */
    public boolean dfs(TreeNode root, int target) {
        if (root == null) return false;
        if (root.left == null && root.right == null) {
            return root.val == target;
        }
        return (root.left != null && dfs(root.left, target - root.val))
                || (root.right != null && dfs(root.right, target - root.val));
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        return dfs(root, targetSum);
    }
}
